{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-10-14T06:57:58.998304Z",
     "start_time": "2025-10-14T06:57:58.963293Z"
    }
   },
   "source": [
    "# DataFrame的创建方式\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from hyperframe.frame import DataFrame\n",
    "\n",
    "# 通过Series来创建\n",
    "s1 = pd.Series([1,2,3,4,5])\n",
    "s2 = pd.Series([6,7,8,9,10])\n",
    "df = pd.DataFrame({'第一列':s1,'第二列':s2})\n",
    "print(type(df['第一列']))\n",
    "# 通过字典创建\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        'name': ['tom','jack','alice','bob','allen'],\n",
    "        'age':[15,17,20,26,30],\n",
    "        'score':[60.5,80,30.6,70,83.5]\n",
    "    },index=[1,2,3,4,5],columns=['name','score','age']\n",
    ")\n",
    "df"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.series.Series'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "    name  score  age\n",
       "1    tom   60.5   15\n",
       "2   jack   80.0   17\n",
       "3  alice   30.6   20\n",
       "4    bob   70.0   26\n",
       "5  allen   83.5   30"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>score</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>tom</td>\n",
       "      <td>60.5</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>jack</td>\n",
       "      <td>80.0</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>alice</td>\n",
       "      <td>30.6</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>bob</td>\n",
       "      <td>70.0</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>allen</td>\n",
       "      <td>83.5</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:04:00.248688Z",
     "start_time": "2025-10-14T07:04:00.235189Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame的属性\n",
    "print('行索引：')\n",
    "print(df.index)\n",
    "print('列标签：')\n",
    "print(df.columns)\n",
    "print('值')\n",
    "print(df.values)"
   ],
   "id": "fa35c73daf101ab",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "行索引：\n",
      "Int64Index([1, 2, 3, 4, 5], dtype='int64')\n",
      "列标签：\n",
      "Index(['name', 'score', 'age'], dtype='object')\n",
      "值\n",
      "[['tom' 60.5 15]\n",
      " ['jack' 80.0 17]\n",
      " ['alice' 30.6 20]\n",
      " ['bob' 70.0 26]\n",
      " ['allen' 83.5 30]]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:15:22.752633Z",
     "start_time": "2025-10-14T07:15:22.737105Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('维度：',df.ndim)\n",
    "print('形状：',df.shape)\n",
    "print('元素个数：',df.size)\n",
    "print('数据类型：')\n",
    "print(df.dtypes)\n",
    "print(type(df.dtypes))"
   ],
   "id": "d71d5d370c804dd0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "维度： 2\n",
      "形状： (5, 3)\n",
      "元素个数： 15\n",
      "数据类型：\n",
      "name      object\n",
      "score    float64\n",
      "age        int64\n",
      "dtype: object\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:08:50.088229Z",
     "start_time": "2025-10-14T07:08:50.064208Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 行列转置\n",
    "print(df.T)\n",
    "print(df.T.index)\n",
    "print(df.T.dtypes)"
   ],
   "id": "991bc855746a4ae3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          1     2      3     4      5\n",
      "name    tom  jack  alice   bob  allen\n",
      "score  60.5  80.0   30.6  70.0   83.5\n",
      "age      15    17     20    26     30\n",
      "Index(['name', 'score', 'age'], dtype='object')\n",
      "1    object\n",
      "2    object\n",
      "3    object\n",
      "4    object\n",
      "5    object\n",
      "dtype: object\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:09:02.973866Z",
     "start_time": "2025-10-14T07:09:02.949807Z"
    }
   },
   "cell_type": "code",
   "source": "df",
   "id": "f9aea776329b7fd4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    name  score  age\n",
       "1    tom   60.5   15\n",
       "2   jack   80.0   17\n",
       "3  alice   30.6   20\n",
       "4    bob   70.0   26\n",
       "5  allen   83.5   30"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>score</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>tom</td>\n",
       "      <td>60.5</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>jack</td>\n",
       "      <td>80.0</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>alice</td>\n",
       "      <td>30.6</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>bob</td>\n",
       "      <td>70.0</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>allen</td>\n",
       "      <td>83.5</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:09:51.010015Z",
     "start_time": "2025-10-14T07:09:51.001525Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取元素 loc iloc at iat\n",
    "# 某行\n",
    "print(df.loc[4]) #显示索引，按标签\n",
    "print(df.iloc[4]) # 隐式索引，从0开始"
   ],
   "id": "31c066b342e5d1e4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name      bob\n",
      "score    70.0\n",
      "age        26\n",
      "Name: 4, dtype: object\n",
      "name     allen\n",
      "score     83.5\n",
      "age         30\n",
      "Name: 5, dtype: object\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:12:15.198039Z",
     "start_time": "2025-10-14T07:12:15.181404Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 某列\n",
    "print(df.loc[:,'name'])\n",
    "print(df.iloc[:,0])"
   ],
   "id": "7efe248459fd6ed4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1      tom\n",
      "2     jack\n",
      "3    alice\n",
      "4      bob\n",
      "5    allen\n",
      "Name: name, dtype: object\n",
      "1      tom\n",
      "2     jack\n",
      "3    alice\n",
      "4      bob\n",
      "5    allen\n",
      "Name: name, dtype: object\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:14:10.035782Z",
     "start_time": "2025-10-14T07:14:10.015325Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取单个元素\n",
    "print(df.at[3,'score'])\n",
    "print(df.iat[2,1])\n",
    "print(df.loc[3,'score'])\n",
    "print(df.iloc[2,1])"
   ],
   "id": "6ac9521b5987b17a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30.6\n",
      "30.6\n",
      "30.6\n",
      "30.6\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:17:41.549635Z",
     "start_time": "2025-10-14T07:17:41.532640Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取单列数据\n",
    "print(df['name'])\n",
    "print(type(df['name']))\n",
    "print(df.name)\n",
    "print(type(df.name))\n",
    "print(df[['name']])\n",
    "print(type(df[['name']]))"
   ],
   "id": "3aa37e7068f53995",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1      tom\n",
      "2     jack\n",
      "3    alice\n",
      "4      bob\n",
      "5    allen\n",
      "Name: name, dtype: object\n",
      "<class 'pandas.core.series.Series'>\n",
      "1      tom\n",
      "2     jack\n",
      "3    alice\n",
      "4      bob\n",
      "5    allen\n",
      "Name: name, dtype: object\n",
      "<class 'pandas.core.series.Series'>\n",
      "    name\n",
      "1    tom\n",
      "2   jack\n",
      "3  alice\n",
      "4    bob\n",
      "5  allen\n",
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:26:18.463015Z",
     "start_time": "2025-10-14T07:26:18.429591Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 多列数据的获取\n",
    "print(df[['name','score']])"
   ],
   "id": "a5716c3c327ba87a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score\n",
      "1    tom   60.5\n",
      "2   jack   80.0\n",
      "3  alice   30.6\n",
      "4    bob   70.0\n",
      "5  allen   83.5\n"
     ]
    }
   ],
   "execution_count": 46
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:28:07.767450Z",
     "start_time": "2025-10-14T07:28:07.744684Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查看部分数据\n",
    "print(df.head(2))\n",
    "print(df.tail(2))"
   ],
   "id": "33e5fc1ea002a5e1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   name  score  age\n",
      "1   tom   60.5   15\n",
      "2  jack   80.0   17\n",
      "    name  score  age\n",
      "4    bob   70.0   26\n",
      "5  allen   83.5   30\n"
     ]
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:31:25.170402Z",
     "start_time": "2025-10-14T07:31:25.148879Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 布尔索引筛选数据\n",
    "df.score>70\n",
    "df[(df['score']>70) & (df['age']<20)]"
   ],
   "id": "7c6d55727a5f77b0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   name  score  age\n",
       "2  jack   80.0   17"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>score</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>jack</td>\n",
       "      <td>80.0</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:31:51.726939Z",
     "start_time": "2025-10-14T07:31:51.708923Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 随机抽样\n",
    "df.sample(3)"
   ],
   "id": "7b254239e2a995f0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    name  score  age\n",
       "5  allen   83.5   30\n",
       "1    tom   60.5   15\n",
       "4    bob   70.0   26"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>score</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>allen</td>\n",
       "      <td>83.5</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>tom</td>\n",
       "      <td>60.5</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>bob</td>\n",
       "      <td>70.0</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "df = pd.DataFrame(\n",
    "    {\n",
    "        'name': ['tom','jack','alice','bob','allen'],\n",
    "        'age':[15,17,20,26,30],\n",
    "        'score':[60.5,80,30.6,70,83.5]\n",
    "    },index=[1,2,3,4,5],columns=['name','score','age']\n",
    ")"
   ],
   "id": "5d98546c14d41321"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "print(df.head()) # 查看前n行数据，默认5行\n",
    "print(df.tail()) # 查看后n行数据，默认5行"
   ],
   "id": "63a9acbc9ab8918a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:46:26.039533Z",
     "start_time": "2025-10-14T07:46:26.019547Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.isin(['jack',20])) # 查看元素是否包含在参数集合中",
   "id": "59236236b7884213",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score    age\n",
      "1  False  False  False\n",
      "2   True  False  False\n",
      "3  False  False   True\n",
      "4  False  False  False\n",
      "5  False  False  False\n"
     ]
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:47:58.548517Z",
     "start_time": "2025-10-14T07:47:58.537013Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.isna()) # 查看元素是否是缺失值",
   "id": "47bfd1bf4927d803",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score    age\n",
      "1  False  False  False\n",
      "2  False  False  False\n",
      "3  False  False  False\n",
      "4  False  False  False\n",
      "5  False  False  False\n"
     ]
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:53:11.713588Z",
     "start_time": "2025-10-14T07:53:11.704093Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df['score'].sum()) # 某一列总和\n",
    "print(df.score.max())# 最小值\n",
    "print(df.age.min()) # 最小值\n",
    "print(df.score.mean()) # 平均数\n",
    "print(df.score.median()) # 中位数\n",
    "print(df.score.mode()) # 众数"
   ],
   "id": "bc072f9ac98525ec",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "324.6\n",
      "83.5\n",
      "15\n",
      "64.92\n",
      "70.0\n",
      "0    30.6\n",
      "1    60.5\n",
      "2    70.0\n",
      "3    80.0\n",
      "4    83.5\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 65
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:58:31.687181Z",
     "start_time": "2025-10-14T07:58:31.676676Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.DataFrame(\n",
    "    {\n",
    "        'name': ['tom','jack','alice','bob','allen'],\n",
    "        'age':[15,15,20,26,30],\n",
    "        'score':[60.5,80,30.6,70,83.5]\n",
    "    },index=[1,2,3,4,5],columns=['name','score','age']\n",
    ")"
   ],
   "id": "4b12e0f7924d33e3",
   "outputs": [],
   "execution_count": 67
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T07:58:45.236937Z",
     "start_time": "2025-10-14T07:58:45.218943Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df.score.var()) # 方差\n",
    "print(df.score.std()) # 标准差\n",
    "print(df.score.quantile(0.25))  # 分位数"
   ],
   "id": "d1b520d7e5fbad8a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "448.957\n",
      "21.188605428390044\n"
     ]
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T08:00:31.242050Z",
     "start_time": "2025-10-14T08:00:31.205021Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.describe())",
   "id": "eb3ea86afb8b3983",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           score        age\n",
      "count   5.000000   5.000000\n",
      "mean   64.920000  21.200000\n",
      "std    21.188605   6.685806\n",
      "min    30.600000  15.000000\n",
      "25%    60.500000  15.000000\n",
      "50%    70.000000  20.000000\n",
      "75%    80.000000  26.000000\n",
      "max    83.500000  30.000000\n"
     ]
    }
   ],
   "execution_count": 69
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T08:01:13.790514Z",
     "start_time": "2025-10-14T08:01:13.770534Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.count()) # 每一列非缺失值的个数",
   "id": "eb9619601b61d2d0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name     5\n",
      "score    5\n",
      "age      5\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 70
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T08:02:24.380910Z",
     "start_time": "2025-10-14T08:02:24.360920Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.value_counts()) # 出现的次数 一行为一条记录判重",
   "id": "c94febf044968c7e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name   score  age\n",
      "alice  30.6   20     1\n",
      "allen  83.5   30     1\n",
      "bob    70.0   26     1\n",
      "jack   80.0   15     1\n",
      "tom    60.5   15     1\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 73
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T08:08:23.807214Z",
     "start_time": "2025-10-14T08:08:23.792681Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df.drop_duplicates())\n",
    "print(df.drop_duplicates(subset=['age']))"
   ],
   "id": "e6fb1af9f789cdb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score  age\n",
      "1    tom   60.5   15\n",
      "3  alice   30.6   20\n",
      "4    bob   70.0   26\n",
      "5  allen   83.5   30\n"
     ]
    }
   ],
   "execution_count": 79
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T08:04:01.080752Z",
     "start_time": "2025-10-14T08:04:01.071223Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.duplicated(subset=['age']))",
   "id": "250969cf68595e23",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1    False\n",
      "2     True\n",
      "3    False\n",
      "4    False\n",
      "5    False\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 78
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T08:08:39.524675Z",
     "start_time": "2025-10-14T08:08:39.506155Z"
    }
   },
   "cell_type": "code",
   "source": "df.sample(2) # 随机抽样",
   "id": "b590c624b2c33114",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    name  score  age\n",
       "2   jack   80.0   15\n",
       "3  alice   30.6   20"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>score</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>jack</td>\n",
       "      <td>80.0</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>alice</td>\n",
       "      <td>30.6</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 80
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T08:09:09.100785Z",
     "start_time": "2025-10-14T08:09:09.088270Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.replace(15,30)) # 替换",
   "id": "fe6362b1a3a04b39",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score  age\n",
      "1    tom   60.5   30\n",
      "2   jack   80.0   30\n",
      "3  alice   30.6   20\n",
      "4    bob   70.0   26\n",
      "5  allen   83.5   30\n"
     ]
    }
   ],
   "execution_count": 81
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T08:13:00.411223Z",
     "start_time": "2025-10-14T08:13:00.400719Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df.cumsum() # 累加\n",
    "df.cummax() # 累积最大\n",
    "df.cummin(axis=0) # 累积最小 axis=0按列累积 axis=1按行累积"
   ],
   "id": "35ca59e39e3d29a5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    name  score  age\n",
       "1    tom   60.5   15\n",
       "2   jack   60.5   15\n",
       "3  alice   30.6   15\n",
       "4  alice   30.6   15\n",
       "5  alice   30.6   15"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>score</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>tom</td>\n",
       "      <td>60.5</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>jack</td>\n",
       "      <td>60.5</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>alice</td>\n",
       "      <td>30.6</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>alice</td>\n",
       "      <td>30.6</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>alice</td>\n",
       "      <td>30.6</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 85
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T08:58:05.106875Z",
     "start_time": "2025-10-14T08:58:05.088894Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df.sort_index(ascending=False))\n",
    "print(df.sort_values(by='score'))"
   ],
   "id": "d5b0b13b05ca4d98",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score  age\n",
      "5  allen   83.5   30\n",
      "4    bob   70.0   26\n",
      "3  alice   30.6   20\n",
      "2   jack   80.0   15\n",
      "1    tom   60.5   15\n",
      "    name  score  age\n",
      "3  alice   30.6   20\n",
      "1    tom   60.5   15\n",
      "4    bob   70.0   26\n",
      "2   jack   80.0   15\n",
      "5  allen   83.5   30\n"
     ]
    }
   ],
   "execution_count": 89
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T09:03:01.167731Z",
     "start_time": "2025-10-14T09:03:01.143272Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.DataFrame(\n",
    "    {\n",
    "        'name': ['tom','jack','alice','bob','allen'],\n",
    "        'age':[15,15,20,26,30],\n",
    "        'score':[60.5,80,70,70,83.5]\n",
    "    },index=[1,2,3,4,5],columns=['name','score','age']\n",
    ")"
   ],
   "id": "33021adfcab4e2cd",
   "outputs": [],
   "execution_count": 93
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T09:03:03.297865Z",
     "start_time": "2025-10-14T09:03:03.285898Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.sort_values(by=['score','age'],ascending=[False,True]))",
   "id": "727b97c8df9d51c6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score  age\n",
      "5  allen   83.5   30\n",
      "2   jack   80.0   15\n",
      "3  alice   70.0   20\n",
      "4    bob   70.0   26\n",
      "1    tom   60.5   15\n"
     ]
    }
   ],
   "execution_count": 94
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "df = pd.DataFrame(\n",
    "    {\n",
    "        'name': ['tom','jack','alice','bob','allen'],\n",
    "        'age':[15,15,20,26,30],\n",
    "        'score':[60.5,80,80,70,83.5]\n",
    "    },index=[1,2,3,4,5],columns=['name','score','age']\n",
    ")"
   ],
   "id": "b747bf3b8969772c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-14T09:04:29.660317Z",
     "start_time": "2025-10-14T09:04:29.641367Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df.nlargest(2,columns=['score','age']))\n",
    "print(df.nsmallest(2,columns=['score','age']))"
   ],
   "id": "7f08ade289e114a7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score  age\n",
      "5  allen   83.5   30\n",
      "2   jack   80.0   15\n",
      "    name  score  age\n",
      "1    tom   60.5   15\n",
      "3  alice   70.0   20\n"
     ]
    }
   ],
   "execution_count": 97
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "案例1：学生成绩分析\n",
    "场景：某班级的学生成绩数据如下，请完成以下任务：\n",
    "1.计算每位学生的总分和平均分。\n",
    "2.找出数学成绩高于90分或英语成绩高于85分的学生。\n",
    "3.按总分从高到低排序，并输出前3名学生\n",
    "'''"
   ],
   "id": "d66b99bd12cd078b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-15T03:22:04.639429Z",
     "start_time": "2025-10-15T03:22:04.575875Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = {\n",
    "    '姓名': ['张三','李四','王五','赵六','钱七'],\n",
    "    '数学': [85,92,78,88,95],\n",
    "    '英语': [90,88,85,92,80],\n",
    "    '物理': [75,80,88,85,90]\n",
    "}\n",
    "\n",
    "scores = pd.DataFrame(data)\n",
    "scores"
   ],
   "id": "926df985e2667de5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   姓名  数学  英语  物理\n",
       "0  张三  85  90  75\n",
       "1  李四  92  88  80\n",
       "2  王五  78  85  88\n",
       "3  赵六  88  92  85\n",
       "4  钱七  95  80  90"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>物理</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>张三</td>\n",
       "      <td>85</td>\n",
       "      <td>90</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>李四</td>\n",
       "      <td>92</td>\n",
       "      <td>88</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>王五</td>\n",
       "      <td>78</td>\n",
       "      <td>85</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>赵六</td>\n",
       "      <td>88</td>\n",
       "      <td>92</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>钱七</td>\n",
       "      <td>95</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-15T03:31:54.728756Z",
     "start_time": "2025-10-15T03:31:54.699844Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# 1.计算每位学生的总分和平均分。\n",
    "scores['总分'] = scores[['数学','英语','物理']].sum(axis=1)\n",
    "scores['平均分'] = scores['总分'] / 3\n",
    "scores['平均分2'] = scores[['数学','英语','物理']].mean(axis=1)\n",
    "scores\n",
    "# 2.找出数学成绩高于90分或英语成绩高于85分的学生。\n",
    "scores[(scores['数学']>90)|(scores['英语']>85)]\n",
    "# 3.按总分从高到低排序，并输出前3名学生\n",
    "r1 = scores.sort_values('总分',ascending=False).head(3)\n",
    "r2 = scores.nlargest(3,columns=['总分'])\n",
    "print(r1)\n",
    "print(r2)"
   ],
   "id": "b66f829bad9dd516",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   姓名  数学  英语  物理   总分        平均分       平均分2\n",
      "3  赵六  88  92  85  265  88.333333  88.333333\n",
      "4  钱七  95  80  90  265  88.333333  88.333333\n",
      "1  李四  92  88  80  260  86.666667  86.666667\n",
      "   姓名  数学  英语  物理   总分        平均分       平均分2\n",
      "3  赵六  88  92  85  265  88.333333  88.333333\n",
      "4  钱七  95  80  90  265  88.333333  88.333333\n",
      "1  李四  92  88  80  260  86.666667  86.666667\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "案例2：销售数据分析\n",
    "场景：某公司销售数据如下，请完成以下任务：\n",
    "1.计算每种产品的总销售额（销售额 = 单价 * 销量）\n",
    "2.找出销售额最高的产品。\n",
    "3.按销售额从高到低排序，并输出所有产品信息。\n",
    "'''"
   ],
   "id": "88c0e7f47c0ea309"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-15T03:39:18.808524Z",
     "start_time": "2025-10-15T03:39:18.782402Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = {\n",
    "    '产品名称': ['A','B','C','D'],\n",
    "    '单价': [100,150,200,120],\n",
    "    '销量': [50,30,20,40]\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "#1.计算每种产品的总销售额（销售额 = 单价 * 销量）\n",
    "df['总销售额'] = df['单价'] * df['销量']\n",
    "df\n",
    "#2.找出销售额最高的产品。\n",
    "df.nlargest(1,'总销售额')\n",
    "#3.按销售额从高到低排序，并输出所有产品信息。\n",
    "df.sort_values('总销售额',ascending=False)\n",
    "\n"
   ],
   "id": "7526088dcf0805a2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  产品名称   单价  销量  总销售额\n",
       "0    A  100  50  5000\n",
       "3    D  120  40  4800\n",
       "1    B  150  30  4500\n",
       "2    C  200  20  4000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>产品名称</th>\n",
       "      <th>单价</th>\n",
       "      <th>销量</th>\n",
       "      <th>总销售额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>100</td>\n",
       "      <td>50</td>\n",
       "      <td>5000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>D</td>\n",
       "      <td>120</td>\n",
       "      <td>40</td>\n",
       "      <td>4800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>B</td>\n",
       "      <td>150</td>\n",
       "      <td>30</td>\n",
       "      <td>4500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>C</td>\n",
       "      <td>200</td>\n",
       "      <td>20</td>\n",
       "      <td>4000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-15T03:40:50.333120Z",
     "start_time": "2025-10-15T03:40:50.313175Z"
    }
   },
   "cell_type": "code",
   "source": [
    "'''\n",
    "案例3：电商用户行为分析\n",
    "场景：某电商平台的用户行为数据如下，请完成以下任务：\n",
    "1.计算每位用户的总消费金额（消费金额 = 商品单价 * 购买数量）\n",
    "2.找出消费金额最高的用户，并输出其所有信息\n",
    "3.计算所有用户的平均消费金额（保留2位小数）\n",
    "4.统计电子产品的总购买数量\n",
    "'''"
   ],
   "id": "5f7d0d102d9fd1e5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n案例3：电商用户行为分析\\n场景：某电商平台的用户行为数据如下，请完成以下任务：\\n1.计算每位用户的总消费金额（消费金额 = 商品单价 * 购买数量）\\n'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-15T03:53:16.256178Z",
     "start_time": "2025-10-15T03:53:16.245695Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = {\n",
    "    '用户ID': [101,102,103,104,105],\n",
    "    '用户名': ['Alice','Bob','Charlie','David','Eve'],\n",
    "    '商品类别': ['电子产品','服饰','电子产品','家居','服饰'],\n",
    "    '商品单价': [1200,300,800,150,200],\n",
    "    '购买数量': [1,3,2,5,4]\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "#1.计算每位用户的总消费金额（消费金额 = 商品单价 * 购买数量）\n",
    "df['总消费金额'] = df['商品单价'] * df['购买数量']\n",
    "df\n",
    "#2.找出消费金额最高的用户，并输出其所有信息\n",
    "df.nlargest(1,columns='总消费金额')\n",
    "#3.计算所有用户的平均消费金额（保留2位小数）\n",
    "df['总消费金额'].mean()\n",
    "#4.统计电子产品的总购买数量\n",
    "df[df['商品类别'] == '电子产品']['购买数量'].sum()\n"
   ],
   "id": "272b1e3d1e8c9cbb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1050.0"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 34
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
